package com.pvdnc.systemdebugger.userauth;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;

import com.pvdnc.systemdebugger.compacter.utils.AppContext;

public class PasswordDialog {
    private static final String TAG="PasswordDialog";

    private void ensureLooper(){
        if(Looper.myLooper()==null){
            Looper.prepare();
            Log.d(TAG,"looper has been prepared");
        }
    }

    @SuppressLint("HandlerLeak")
    public PasswordDialog(){
        ensureLooper();
        mHandler= new Handler() {
            @Override
            public void handleMessage(Message mesg) {
                // process incoming messages here
                //super.handleMessage(msg);
                throw new RuntimeException();
            }
        };
    }

    volatile int dialogResult=-1;

    Handler mHandler;


    public int getDialogResult()
    {
        return dialogResult;
    }
    public void setDialogResult(int dialogResult)
    {
        this.dialogResult = dialogResult;
    }


    private void resume(){
        Message m = mHandler.obtainMessage();
        mHandler.sendMessage(m);
    }

    public int showDialog(String caller,String operation)
    {
        AlertDialog.Builder builder=new AlertDialog.Builder(AppContext.getAppContext());
        builder.setMessage("caller:"+caller+"\n"+"operation:"+operation);
        builder.setPositiveButton("允许", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                setDialogResult(1);
                resume();
            }
        });
        builder.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                setDialogResult(0);
                resume();
            }
        });

        //ensureLooper();//重新准备Looper
        final AlertDialog dialog=builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.setCanceledOnTouchOutside(false);//点击屏幕不消失
        dialog.show();
        new Thread(){
            @Override
            public void run() {
                int countDown=15;
                while (dialogResult==-1) {
                    countDown = countDown - 1;
                    if (countDown <= 0) {
                        Log.e(TAG, "15 seconds count down reached");
                        dialog.dismiss();
                        setDialogResult(0);//按照默认政策，超时拒绝
                        PasswordDialog.this.resume();
                        break;
                    }
                    if(dialogResult!=-1)
                        break;//保险起见，休眠1s前再检查一遍
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        //阻塞直到确认
        try {
            Looper.getMainLooper().loop();
        }
        catch(RuntimeException e2)
        {
            Log.d(TAG,"recover from waiting loop");
        }
        return dialogResult;
    }

}
